# Read the data file and run analyses.

setwd("F:/Experiment Data/Complex Contagion/ComplexContagionDatasets/")
exp2 = read.csv("Exp2_polarizing_questions.csv")

# Sanity checks.
head(exp2)
dim(exp2)
table(exp2$finished)
table(exp2$conditionName)

library(xtable)

# Code dependent variables.
# Code Self, Schis and Other columns.
exp2$Self = 0
exp2[exp2$userChoice == "Keep_10_cents_and_Do_Not_Donate", c("Self")] = 1
exp2$Schis = 0
exp2[exp2$userChoice == "Schistosomiasis_Control_Initiative", c("Schis")] = 1
exp2$Other = 0
exp2[exp2$Self == 0 & exp2$Schis == 0, c("Other")] = 1

# Code AnyCharity column.
exp2$AnyCharity = 0
exp2[exp2$userChoice != "Keep_10_cents_and_Do_Not_Donate", c("AnyCharity")] = 1

# Code independent variables.
# Code Tie Strength column.
exp2$Similarity = "High"
exp2[grepl("^AllLo__", exp2$conditionName), c("Similarity")] = "Low"
#exp2[, c("conditionName", "Similarity")]

# Code SelfNeighbors, SchisNeighbors and OtherNeighbors columns.
exp2$SelfNeighbors = 0
exp2[grepl("Self_1", exp2$conditionName), c("SelfNeighbors")] = 1
exp2[grepl("Self_2", exp2$conditionName), c("SelfNeighbors")] = 2
exp2[grepl("Self_3", exp2$conditionName), c("SelfNeighbors")] = 3

exp2$SchisNeighbors = 0
exp2[grepl("Schis_1", exp2$conditionName), c("SchisNeighbors")] = 1
exp2[grepl("Schis_2", exp2$conditionName), c("SchisNeighbors")] = 2
exp2[grepl("Schis_3", exp2$conditionName), c("SchisNeighbors")] = 3

exp2$OtherNeighbors = 0
exp2[grepl("Rand_1", exp2$conditionName), c("OtherNeighbors")] = 1
exp2[grepl("Rand_2", exp2$conditionName), c("OtherNeighbors")] = 2
exp2[grepl("Rand_3", exp2$conditionName), c("OtherNeighbors")] = 3

exp2$DonatingNeighbors = 3 - exp2$SelfNeighbors
#exp2$anyone = 3 - exp2$SelfNeighbors
#exp2$anytwo = exp2$SchisNeighbors + exp2$OtherNeighbors
#exp2$anynoo = exp2$anyone - exp2$anytwo

exp2[, c("conditionName", "Similarity", "SelfNeighbors", "SchisNeighbors", "OtherNeighbors")]


# Run regressions.
# Influence only model.
influenceModel = glm(Schis ~ SchisNeighbors, family=binomial(link='logit'), data=exp2)
summary(influenceModel)

# Influence with tie-strength interaction model.
influenceSimilarityModel = glm(Schis ~ SchisNeighbors * Similarity, family=binomial(link='logit'), data=exp2)
summary(influenceSimilarityModel)

# Demographics only model.
demographicsModel = glm(Schis ~ gender + income + year_of_birth + smoking + drinking + mturk, family=binomial(link='logit'), data=exp2)
summary(demographicsModel)

# Influence + Tie Strength + Demographics model.
influenceDemographicsModel = glm(Schis ~ SchisNeighbors * Similarity + gender + income + year_of_birth + smoking + drinking + mturk, family=binomial(link='logit'), data=exp2)
summary(influenceDemographicsModel)


# Questions only model.
questionsModel = glm(Schis ~ restrict_gun_ownership + climate_change + race_discrimination + homosexuality + women_obstacles + abortion_murder + muslim_immigrants + christian_nation + school_prayer + russian_influence, family=binomial(link='logit'), data=exp2)
summary(questionsModel)

# Influence + Tie Strength + Questions model.
influenceQuestionsModel = glm(Schis ~ SchisNeighbors * Similarity + restrict_gun_ownership + climate_change + race_discrimination + homosexuality + women_obstacles + abortion_murder + muslim_immigrants + christian_nation + school_prayer + russian_influence, family=binomial(link='logit'), data=exp2)
summary(influenceQuestionsModel)

# Use questions as a liberal--conservative scale.
# First construct scale.
exp2$ConservativeAnswers = 0
exp2$ConservativeAnswers = exp2$ConservativeAnswers + ifelse(exp2$restrict_gun_ownership == "no", 1, 0)
exp2$ConservativeAnswers = exp2$ConservativeAnswers + ifelse(exp2$climate_change == "no_big_deal", 1, 0)
exp2$ConservativeAnswers = exp2$ConservativeAnswers + ifelse(exp2$race_discrimination == "no_discrimination", 1, 0)
exp2$ConservativeAnswers = exp2$ConservativeAnswers + ifelse(exp2$homosexuality == "discourage", 1, 0)
exp2$ConservativeAnswers = exp2$ConservativeAnswers + ifelse(exp2$women_obstacles == "women_obstacles_no", 1, 0)
exp2$ConservativeAnswers = exp2$ConservativeAnswers + ifelse(exp2$abortion_murder == "yes", 1, 0)
exp2$ConservativeAnswers = exp2$ConservativeAnswers + ifelse(exp2$muslim_immigrants == "no", 1, 0)
exp2$ConservativeAnswers = exp2$ConservativeAnswers + ifelse(exp2$christian_nation == "yes", 1, 0)
exp2$ConservativeAnswers = exp2$ConservativeAnswers + ifelse(exp2$school_prayer == "yes", 1, 0)
exp2$ConservativeAnswers = exp2$ConservativeAnswers + ifelse(exp2$russian_influence == "no", 1, 0)

# Now try different models with that scale.
# Political scale only model.
scaleModel = glm(Schis ~ ConservativeAnswers, family=binomial(link='logit'), data=exp2)
summary(scaleModel)

# Influence with political scale model.
scaleInfluenceModel = glm(Schis ~ SchisNeighbors * ConservativeAnswers, family=binomial(link='logit'), data=exp2)
summary(scaleInfluenceModel)

# Influence + Tie Strength + political scale model.
scaleInfluenceSimilarityModel = glm(Schis ~ SchisNeighbors * Similarity * ConservativeAnswers, family=binomial(link='logit'), data=exp2)
summary(scaleInfluenceSimilarityModel)

# Influence with tie-strength interaction model.
influenceSimilarityModel = glm(Schis ~ SchisNeighbors * Similarity, family=binomial(link='logit'), data=exp2)
summary(influenceSimilarityModel)
xtable(summary(influenceSimilarityModel))

## Influence + Tie Strength + Demographics + Questions model.
kitchenSinkModel = glm(Schis ~ SchisNeighbors * Similarity + gender + income + year_of_birth + smoking + drinking + mturk + restrict_gun_ownership + climate_change + race_discrimination + homosexuality + women_obstacles + abortion_murder + muslim_immigrants + christian_nation + school_prayer + russian_influence, family=binomial(link='logit'), data=exp2)
summary(kitchenSinkModel)
xtable(summary(kitchenSinkModel))


### Switch to Keeping Money for Self outcome and Self neighbor count predictor.
# Influence only model.
influenceModelSelf = glm(Self ~ SelfNeighbors, family=binomial(link='logit'), data=exp2)
summary(influenceModelSelf)

# Influence with tie-strength interaction model.
influenceSimilarityModelSelf = glm(Self ~ SelfNeighbors * Similarity, family=binomial(link='logit'), data=exp2)
summary(influenceSimilarityModelSelf)

# Demographics only model.
demographicsModelSelf = glm(Self ~ gender + income + year_of_birth + smoking + drinking + mturk, family=binomial(link='logit'), data=exp2)
summary(demographicsModelSelf)

# Influence + Tie Strength + Demographics model.
influenceDemographicsModelSelf = glm(Self ~ SelfNeighbors * Similarity + gender + income + year_of_birth + smoking + drinking + mturk, family=binomial(link='logit'), data=exp2)
summary(influenceDemographicsModelSelf)


# Questions only model.
questionsModelSelf = glm(Self ~ restrict_gun_ownership + climate_change + race_discrimination + homosexuality + women_obstacles + abortion_murder + muslim_immigrants + christian_nation + school_prayer + russian_influence, family=binomial(link='logit'), data=exp2)
summary(questionsModelSelf)

# Influence + Tie Strength + Questions model.
influenceQuestionsModelSelf = glm(Self ~ SelfNeighbors * Similarity + restrict_gun_ownership + climate_change + race_discrimination + homosexuality + women_obstacles + abortion_murder + muslim_immigrants + christian_nation + school_prayer + russian_influence, family=binomial(link='logit'), data=exp2)
summary(influenceQuestionsModelSelf)


# Political scale only model.
scaleModelSelf = glm(Self ~ ConservativeAnswers, family=binomial(link='logit'), data=exp2)
summary(scaleModelSelf)

# Influence with political scale model.
scaleInfluenceModelSelf = glm(Self ~ SelfNeighbors * ConservativeAnswers, family=binomial(link='logit'), data=exp2)
summary(scaleInfluenceModelSelf)

# Influence + Tie Strength + political scale model.
scaleInfluenceSimilarityModelSelf = glm(Self ~ SelfNeighbors * Similarity * ConservativeAnswers, family=binomial(link='logit'), data=exp2)
summary(scaleInfluenceSimilarityModelSelf)



### Switch to Donating to Any Charity outcome and DonatingNeighbors count predictor.
## Influence + Tie Strength + Demographics + Questions model.
kitchenSinkModelAnyCharity = glm(AnyCharity ~ DonatingNeighbors * Similarity + gender + income + year_of_birth + smoking + drinking + mturk + restrict_gun_ownership + climate_change + race_discrimination + homosexuality + women_obstacles + abortion_murder + muslim_immigrants + christian_nation + school_prayer + russian_influence, family=binomial(link='logit'), data=exp2)
summary(kitchenSinkModelAnyCharity)
xtable(summary(kitchenSinkModelAnyCharity))

## Influence + Similarity with interaction model.
influenceSimilarityInteractionAnyCharity = glm(AnyCharity ~ DonatingNeighbors * Similarity, family=binomial(link='logit'), data=exp2)
summary(influenceSimilarityInteractionAnyCharity)
xtable(summary(influenceSimilarityInteractionAnyCharity))
